import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class CourseInfArray {
    ArrayList<Course> courseArrayList = new ArrayList<>();
    public void CourseInfArrayInit()
    {
        String CourseInfArray = "src/CourseInfArray";

        try {
            // 读取文件内容到字符串列表
            List<String> lines = Files.readAllLines(Paths.get(CourseInfArray), StandardCharsets.UTF_8);

            // 遍历字符串列表并输出每一行
            for (String line : lines) {
                // 使用空格分隔每行数据
                String[] split = line.split("\\s+");
                if (split.length >= 6) {
                    Course newcourse = new Course();
                    newcourse.setCourseName(split[0]);
                    newcourse.setcID(split[1]);
                    newcourse.setTeacherName(split[2]);
                    newcourse.setTeacherID(split[3]);
                    newcourse.setNormalPercent(split[4]);
                    newcourse.setFinalPercent(split[5]);
                    courseArrayList.add(newcourse);
                    // 其他操作
                } else {
                    // 处理不符合格式的行
                    continue;
                }
            }
        } catch (IOException e) {
            // 处理异常

        }
    }

    public void CourseInfArrayUpdate() {
        String CourseInfArray = "src/CourseInfArray";
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(CourseInfArray))) {
            // 遍历HashMap并将键值对写入文件
            for (Course course : courseArrayList) {
                writer.write(course.getCourseName() + " ");
                writer.write(course.getcID() + " ");
                writer.write(course.getTeacherName() + " ");
                writer.write(course.getTeacherID() + " " );
                writer.write(course.getNormalPercent() + " " );
                writer.write(course.getFinalPercent() );
            }
//            System.out.println("ArrayList contents have been written to the file.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //直接返回整个数据表，用户端编代码时要注意
    public ArrayList<Course> courseDataSheetGet(boolean a)
    {
        return courseArrayList;
    }

}
